#!/usr/bin/env python

"""
    Creature.py
    This file contains prototype class for all objects
"""

# Imports
import random
from GameUI import *
from GameMap import *
from Los import *

class Ticker(object):
    def __init__(self):
        self.ticks = 0L
        self.schedule = {}

    def scheduleTurn(self, interval, obj):
        self.schedule.setdefault(self.ticks+interval, []).append(obj)

    def nextTurn(self):
        thingsToDo = self.schedule.pop(self.ticks, [])
        for obj in thingsToDo:
            obj.doTurn()

class Object(object):
    def __init__(self, gameUI, gameMap, gameObjects, posy, posx, char, ticker, color, bolded = False, speed = 1, health = 1):
        self.posy = posy
        self.posx = posx
        self.char = char
        self.ticker = ticker
        self.color = color
        self.bolded = bolded
        self.speed = speed
        self.health = health

        self.gameUI = gameUI
        self.gameMap = gameMap
        self.gameObjects = gameObjects

        self.ticker.scheduleTurn(self.speed, self)
        self.los = Bresenham(self.gameUI, self.gameMap, self.gameObjects)

        self.draw()

    def doTurn(self):
        # need to be implemented by child classes
        pass

    def draw(self):
        self.gameUI.putChar(self.posy, self.posx, self.char, self.color, self.bolded)

    def clear(self):
        if self.gameMap.map[self.posy][self.posx].fov == True:
            self.gameUI.putChar(self.posy, self.posx, self.gameMap.map[self.posy][self.posx].char, self.gameMap.map[self.posy][self.posx].color, False)
        elif self.gameMap.map[self.posy][self.posx].visited == True:
            self.gameUI.putChar(self.posy, self.posx, self.gameMap.map[self.posy][self.posx].char, 3, False)
        else:
            self.gameUI.putChar(self.posy, self.posx, ' ', self.gameMap.map[self.posy][self.posx].color, False)

    def move(self, dy, dx):
        self.posy += dy
        self.posx += dx